//
//  CRInputEmoticonTabView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/8/5.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CRInputEmoticonTabView.h"
#import "CREmoticonManager.h"
#import "UIImage+CRKit.h"

const NSInteger CRInputEmoticonTabViewHeight = 35;
const NSInteger CRInputEmoticonSendButtonWidth = 50;
const CGFloat CRInputLineBoarder = .5f;

@interface CRInputEmoticonTabView()

@property (nonatomic,strong) NSMutableArray * tabs;

@property (nonatomic,strong) NSMutableArray * seps;

@end

#define sepColor [Utils colorHexString:@"0x8A8E93" alpha:1.0]

@implementation CRInputEmoticonTabView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, CRInputEmoticonTabViewHeight)];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp {

    self.tabs = [NSMutableArray array];
    self.seps = [NSMutableArray array];
    
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
    self.sendButton.height = CRInputEmoticonTabViewHeight;
    self.sendButton.width = CRInputEmoticonSendButtonWidth;
    [self addSubview:self.sendButton];
    
    self.layer.borderColor = sepColor.CGColor;
    self.layer.borderWidth =  CRInputLineBoarder;
}

- (void)loadCatalogs:(NSArray*)emoticonCatalogs {
    
    for (UIView *subView in [_tabs arrayByAddingObjectsFromArray:_seps]) {
        [subView removeFromSuperview];
    }
    [_tabs removeAllObjects];
    [_seps removeAllObjects];
    for (CRInputEmoticonCatalog * catelog in emoticonCatalogs) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage cr_emoticonInKit:catelog.icon] forState:UIControlStateNormal];
        [button setImage:[UIImage cr_emoticonInKit:catelog.iconPressed] forState:UIControlStateHighlighted];
        [button setImage:[UIImage cr_emoticonInKit:catelog.iconPressed] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(onTouchTab:) forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        [self addSubview:button];
        [_tabs addObject:button];
        
        UIView *sep = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CRInputLineBoarder, CRInputEmoticonTabViewHeight)];
        sep.backgroundColor = sepColor;
        [_seps addObject:sep];
        [self addSubview:sep];
    }
}

- (void)onTouchTab:(id)sender {
    
    NSInteger index = [self.tabs indexOfObject:sender];
    [self selectTabIndex:index];
    if ([self.delegate respondsToSelector:@selector(tabView:didSelectTabIndex:)]) {
        [self.delegate tabView:self didSelectTabIndex:index];
    }
}


- (void)selectTabIndex:(NSInteger)index {
    
    for (NSInteger i = 0; i < self.tabs.count ; i++) {
        UIButton *btn = self.tabs[i];
        btn.selected = i == index;
    }
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat spacing = 10;
    CGFloat left    = spacing;
    for (NSInteger index = 0; index < self.tabs.count ; index++) {
        UIButton *button = self.tabs[index];
        button.left = left;
        button.centerY = self.height * .5f;
        
        UIView *sep = self.seps[index];
        sep.left = (int)(button.right + spacing);
        left = (int)(sep.right + spacing);
    }
    _sendButton.right = (int)self.width;
}


@end
